/**
 * Copyright (c) 2009 Jim Hurne
 * 
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use,
 * copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following
 * conditions:
 * 
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 */
package org.tzconverter.patterneditor;

import java.awt.Color;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;

import org.jdesktop.layout.GroupLayout;
import org.tzconverter.gui.GradientButton;
import org.tzconverter.util.Icons;

/**
 * Swing implementation of the PatternEditorView.
 * 
 * 
 * @since Jun 9, 2009
 */

public class PatternEditorPanel extends JPanel implements PatternEditorView
{
  private static final long serialVersionUID = 5640178026740743384L;
  
  private JLabel errorLabel;
  private JLabel patternLabel;
  private JTextField patternField;
  private JButton applyButton;
  
  private final Icons icons;
  
  public PatternEditorPanel(Icons icons) 
  {
    this.icons = icons;
    
    this.setOpaque(false);
    
    createComponents();
    layoutComponents();
  }
  
  private void createComponents()
  {
    errorLabel = new JLabel(" ");
    errorLabel.setIcon(icons.getError());
    errorLabel.setForeground(Color.RED);
    
    applyButton = new GradientButton("Apply");
    applyButton.setIcon(icons.getApply());

    patternField = new JTextField(15);
    patternField.setMaximumSize(patternField.getPreferredSize());
    
    patternField.getDocument().addDocumentListener(new DocumentListener() {
      public void changedUpdate(DocumentEvent event)
      {
        //Do nothing
      }

      public void insertUpdate(DocumentEvent event)
      {
        Document doc = event.getDocument();
        try
        {
          firePropertyChange(PROP_PATTERN, null, doc.getText(0, doc.getLength()));
        }
        catch (BadLocationException e)
        {
          throw new RuntimeException(e.getMessage(), e);
        }       
      }

      public void removeUpdate(DocumentEvent event)
      {
        Document doc = event.getDocument();
        try
        {
          firePropertyChange(PROP_PATTERN, null, doc.getText(0, doc.getLength()));
        }
        catch (BadLocationException e)
        {
          throw new RuntimeException(e.getMessage(), e);
        }               
      }      
    });
    
    patternLabel = new JLabel("Date/Time Pattern:");
    patternLabel.setForeground(Color.WHITE);
    patternLabel.setVisible(true);
  }
  
  private void layoutComponents()
  {
    GroupLayout layout = new GroupLayout(this);
    this.setLayout(layout);
    
    layout.setAutocreateGaps(true);
    layout.setHonorsVisibility(true);
    
    layout.setHorizontalGroup(
        layout.createParallelGroup()
          .add(errorLabel)
          .add(patternLabel)
          .add(
              layout.createSequentialGroup()
                .add(patternField)
                .add(applyButton)
              )
        );
    
    layout.setVerticalGroup(
        layout.createSequentialGroup()
          .add(errorLabel)
          .add(patternLabel)
          .add(
              layout.createParallelGroup(GroupLayout.BASELINE)
                .add(patternField)
                .add(applyButton)
              )
        );
  }
  
  /* (non-Javadoc)
   * @see org.tzconverter.patterneditor.PatternEditorView#showError(java.lang.String)
   */
  public void showError(String error)
  {
    errorLabel.setVisible(true);
    errorLabel.setText(error);
    patternLabel.setVisible(false);
  }

  /* (non-Javadoc)
   * @see org.tzconverter.patterneditor.PatternEditorView#clearError()
   */
  public void clearError()
  {
    errorLabel.setVisible(false);
    errorLabel.setText(" ");
    patternLabel.setVisible(true);
  }

  /* (non-Javadoc)
   * @see org.tzconverter.patterneditor.PatternEditorView#setApplyButtonEnabled(boolean)
   */
  public void setApplyButtonEnabled(boolean enabled)
  {
    applyButton.setEnabled(enabled);
  }

  /* (non-Javadoc)
   * @see org.tzconverter.patterneditor.PatternEditorView#getPattern()
   */
  public String getPattern()
  {
    return patternField.getText();
  }

  /* (non-Javadoc)
   * @see org.tzconverter.patterneditor.PatternEditorView#setPattern(java.lang.String)
   */
  public void setPattern(String pattern)
  {
    patternField.setText(pattern);
  }

  /* (non-Javadoc)
   * @see org.tzconverter.patterneditor.PatternEditorView#addActionListener(java.awt.event.ActionListener)
   */
  public void addActionListener(ActionListener listener)
  {
    applyButton.addActionListener(listener);
  }

  /* (non-Javadoc)
   * @see org.tzconverter.patterneditor.PatternEditorView#removeActionListener(java.awt.event.ActionListener)
   */
  public void removeActionListener(ActionListener listener)
  {
    applyButton.removeActionListener(listener);
  }
}
